--[[
    购买飞飞的具体细节    
--]]
require "Common/define"
require "Logic/Config"
require "Net/send_Msg"
require "Common/functions"
require "Logic/PetManager"

UIPetShopPetDetail = UIPetShopPetDetail or {}
HelperFunc.SetUIEnv(UIPetShopPetDetail)
local this = UIPetShopPetDetail
this.hasInit = false

function this.Init()
    local _name, _packageName = getUIName(PanelNames.UIPetShopPetDetail)
    panelMgr:ShowUI(_name, _packageName, this.onCreate, Config.useLocalUI)
end
function this.onCreate(in_root)
    if not this.hasInit then
        this.hasInit = true
        this.Btn_Back = in_root:GetChild("Btn_Back")
        this.Btn_Back.onClick:Add(this.onClickClose)
        this.Btn_Close = in_root:GetChild("Btn_Close")
        this.Btn_Close.onClick:Add(this.onClickClose)

        this.Txt_DetailPetName = in_root:GetChild("Txt_DetailPetName")
        this.Txt_DetailPlayerName = in_root:GetChild("Txt_DetailPlayerName")
        this.Model_DetailPet = in_root:GetChild("Model_DetailPet")
        this.Group_PetPartItem = {}
        for i = 1, PetManager.PetPart.Ornament do
            local _partIndex = i > 9 and i or "0" .. i
            local _partItem = in_root:GetChild("Btn_PetBodyShow" .. _partIndex)
            table.insert(this.Group_PetPartItem, _partItem)
        end

        this.CtrlDetailType = in_root:GetController("DetailType")
        local _detailTypeIndex = 0
        if BrokerManager.BrokerPetType == BrokerUtils.BrokerPetType.Buy then
            --购买飞飞
            _detailTypeIndex = 0
            in_root:GetChild("Txt_DetailBuyTitle").text = getLanguage("PETSHOP_DetailBuyTitle")
            this.Btn_DetailBuy = in_root:GetChild("Btn_DetailBuy")
            this.Btn_DetailBuy.onClick:Add(this.onClickDetailBuy)
            this.Txt_DetailBuyPrice = in_root:GetChild("Txt_DetailBuyPrice")
        elseif BrokerManager.BrokerPetType == BrokerUtils.BrokerPetType.Evolution then
            --进化飞飞
            _detailTypeIndex = 1
            in_root:GetChild("Txt_DetailEvolutionTitle").text = getLanguage("PETSHOP_DetailEvolutionTitle")
            this.Btn_DetailEvolutionConfirm = in_root:GetChild("Btn_DetailEvolutionConfirm")
            this.Btn_DetailEvolutionConfirm.onClick:Add(this.onClickDetailEvolutionConfirm)
            this.Txt_DetailEvolutionPrice = in_root:GetChild("Txt_DetailEvolutionPrice")
        elseif BrokerManager.BrokerPetType == BrokerUtils.BrokerPetType.Myself then
            in_root:GetChild("Txt_DetailMyConsignmentTitle").text = getLanguage("PETSHOP_DetailMyConsignmentTitle")
            _detailTypeIndex = 2
        end
        this.CtrlDetailType:SetSelectedIndex(_detailTypeIndex)
    end

    if this.detailPetModelInfo == nil then
        this.detailPetModelInfo = {}
    end
    this.showDetailPetModel()
end

function this.OnDisable()
    this.Btn_Back.onClick:Clear()
    this.Btn_Close.onClick:Clear()

    this.detailPetInfo = nil

    BrokerManager.DetailSelectedPetId = 0
    this.detailPetModelInfo = nil
    this.hasInit = false
end

function this.onClickClose()
    this.OnDisable()
    closeUI(PanelNames.UIPetShopPetDetail)
end

function this.onClickDetailBuy()
    if BrokerManager.CheckIsMyPlayer(this.detailPetInfo.playerId) then
        UISysTips.AddMsg(getLanguage("PETSHOP_BuyPetIsMineError"))
    elseif BrokerManager.GetCurrentPetCoin() < tonumber(this.detailPetInfo.sellPrice) then
        UISysTips.AddMsg(getLanguage("PETSHOP_ShortOfPetCoinError"))
    elseif BrokerManager.CheckPetBagMaxCount() then
        UISysTips.AddMsg(getLanguage("PETSHOP_PetBagMaxCountError"))
    else
        showUI(PanelNames.Page_PetApplyConfirm,BrokerUtils.PETSHOP_PetType)
        --BrokerHandler.ReqBrokerGoodsBuy(this.detailPetInfo.goodsId)
    end
end
function this.onClickDetailEvolutionConfirm()
    if #PetManager.PetInfoList <= 0 then
        UISysTips.AddMsg("请先获取飞飞")
        return
    end
    if BrokerManager.CheckIsMyPlayer(this.detailPetInfo.playerId) then
        UISysTips.AddMsg(getLanguage("PETSHOP_EvolutionPetIsMineError"))
    elseif BrokerManager.GetCurrentPetCoin() < tonumber(this.detailPetInfo.evolutionPrice) then
        UISysTips.AddMsg(getLanguage("PETSHOP_ShortOfPetCoinError"))
    else
        local _info = {}
        _info.data = {}
        _info.data.pet = {}
        _info.data.pet[1] = this.detailPetInfo
        _info.data.playerId = this.detailPetInfo.playerId
        _info.data.name = this.detailPetInfo.playerName
        _info.data.pic = this.detailPetInfo.playerPic
        _info.data.picFrame = this.detailPetInfo.playerPicFrame
        PetManager.refreshOtherPetInfo(_info)
        showUI(PanelNames.Page_PetApplyMyRequest)
    end
end


function this.removDetailPetModel()
    if this.detailPetModelInfo then
        if this.detailPetModelInfo.modelGO then
            destroyImmediate(this.detailPetModelInfo.modelGO)
        end
        this.detailPetModelInfo.modelGO = nil
        this.detailPetModelInfo.parentGO = nil
        this.detailPetModelInfo.petId = nil
    end
end
function this.showDetailPetModel()
    this.detailPetInfo = clone(BrokerManager.GetPetInfoByPetId(BrokerManager.DetailSelectedPetId,BrokerManager.BrokerPetType))
    if this.detailPetInfo and #this.detailPetInfo.genes == PetManager.PetPart.Ornament then
        this.Model_DetailPet.visible = true
        this.Txt_DetailPetName.text = this.detailPetInfo.petName
        this.Txt_DetailPlayerName.text = this.detailPetInfo.playerName
        if BrokerManager.BrokerPetType == BrokerUtils.BrokerPetType.Buy then
            this.Txt_DetailBuyPrice.text = this.detailPetInfo.sellPrice
        elseif BrokerManager.BrokerPetType == BrokerUtils.BrokerPetType.Evolution then
            this.Txt_DetailEvolutionPrice.text = this.detailPetInfo.evolutionPrice
        end

        if this.detailPetModelInfo.modelGO then
            this.detailPetModelInfo.petId = this.detailPetInfo.petId
            PetManager.PetModelRefresh(this.detailPetModelInfo, this.detailPetInfo.genes, true)
        else
            this.removDetailPetModel()
            resMgr:LoadModelPrefab(PetManager.PetModelPrefab, function(go)
                this.detailPetModelInfo.modelGO = newObject(go)

                local _comp = this.detailPetModelInfo.modelGO:GetComponent("SkeletonGraphic")
                if _comp then
                    if this.detailPetModelInfo.modelGO then
                        _comp.material = resMgr.Pet_Copy_Mat
                        _comp.material:SetFloat("_StencilComp", 8)
                        _comp.material:SetFloat("_Stencil", 1);
                    end
                end
                
                this.detailPetModelInfo.parentGO = this.Model_DetailPet
                local _modelWrapper = this.detailPetModelInfo.modelWrapper
                this.detailPetModelInfo.modelWrapper = GoWrapper.New()
                this.detailPetModelInfo.petId = this.detailPetInfo.petId
                PetManager.PetModelFirstSet(this.detailPetModelInfo, this.detailPetInfo.genes, true, Vector3.New(0, 0, 0), Vector3.New(2.5, 2.5, 1))
                if _modelWrapper then
                    _modelWrapper:Dispose()
                end
            end)
        end

        local _go
        local _ctrlItem
        for k, v in pairs(this.detailPetInfo.genes) do
            _go = this.Group_PetPartItem[k]
            _ctrlItem = CtrlManager.GetCtrl(PanelNames.PetPartItem)
            _ctrlItem.initData(_go, v);
        end
    else
        --没有宠物信息
        -- this.Txt_detailPetName.text = getLanguage("PetMain_NoPet")
        this.Model_DetailPet.visible = false;
    end
end
return UIPetShopPetDetail